import type { TabbarDataTypes } from "@/common/types/TabbarDataTypes"
import { openWin, type openWinType } from "@/utils/helpers"

export const useTabbar = defineStore('tabbar', () => {
    // tabBar属性
    const tabBarAttrs = ref({
        fixed: true,
        placeholder: true,
        safeAreaInsetBottom: true,
        activeColor: "#1989fa",
        inactiveColor: "#7d7e80",
    })
    // 主页面跳转
    const mainPage = ref([
        'pages/index/index',
        'pages/record/index',
        'pages/poster/list',
        'pages/users/index',
    ])
    // Tabbar数据
    const tabbar = ref<TabbarDataTypes[]>([
        {
            text: '匹配',
            path: 'pages/index/index',
            iconPath: '/static/tab-bar/icon_mate_home_normal@3x.png',
            iconActive: '/static/tab-bar/icon_mate_home_highlighted@3x.png',
        },
        {
            text: '记录',
            path: 'pages/record/index',
            iconPath: '/static/tab-bar/icon_chats_record_normal@3x.png',
            iconActive: '/static/tab-bar/icon_chats_record_highlighted@3x.png',
        },
        {
            text: 'CP海报',
            path: 'pages/poster/list',
            iconPath: '/static/tab-bar/icon_chats_record_normal@3x.png',
            iconActive: '/static/tab-bar/icon_chats_record_highlighted@3x.png',
        },
        {
            text: '我的',
            path: 'pages/users/index',
            iconPath: '/static/tab-bar/icon_user_my_normal@3x.png',
            iconActive: '/static/tab-bar/icon_user_my_highlighted@3x.png',
        },
    ])

    // 获取默认选中
    const getCurrentRoute = () => {
        // 获取当前打开过的页面路由数组
        let routes = getCurrentPages();
        let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
        if (curRoute) {
            return curRoute
        }
        return ''
    }

    // 设置tabBar数据
    const setTabbar = (data: TabbarDataTypes[]) => {        
        if (!data.length) {
            data = tabbar.value
        }        
        tabbar.value = data
    }

    // 获取tabBar数据
    const initTabbar = () => {
        uni.hideTabBar();
        setTabbar([])
    }

    // 点击跳转
    const hanldTabBarClick = (name:string)=>{
        // 第三方跳转
        if (name.includes('http://') || name.includes('https://')) {
            window.location.href = name
            return;
        }
        let openType:openWinType = 'navigateTo';
        if (mainPage.value.includes(name)) {
            openType = 'switchTab';
        }else{
            openType = 'navigateTo'
        }
        openWin({
            url: `/${name}`
        },openType)
    }

    // 导出
    return {
        tabbar,
        tabBarAttrs,
        initTabbar,
        setTabbar,
        getCurrentRoute,
        hanldTabBarClick
    }
})
